package com.xiaochong.tir.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.ExchangeDimensionMiddle;
import com.xiaochong.tir.common.data.po.TimeDimension;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.service.ExchangeDimensionMiddleService;
import com.xiaochong.tir.manager.service.TimeDimensionService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Created by admin on 2018/6/6.
 * 时间维度管理
 */
@Controller
@RequestMapping(ManagerVersion.API_V1_PREFIX + "lat/")
@Api("时间维度管理")
public class TimeDimensionController {

    @Autowired
    private TimeDimensionService timeDimensionService;
    @Autowired
    private ExchangeDimensionMiddleService exchangeDimensionMiddleService;
    @Autowired
    private RedisServer redisServer;
    /**
     * 列表页
     * @return
     */
    @GetMapping("latpage")
    public String latPage(){
        return "/timedimension/timelat";
    }

    /**
     * 列表
     * @return
     */
    @ApiOperation(value = "时间维度列表",notes = "时间维度列表")
    @PostMapping("latlist")
    @ResponseBody
    public ReturnResult<TimeDimension> selTimeDimension(TimeDimension timeDimension,
                                                        @RequestParam(value = "page") Integer pageIndex,
                                                        @RequestParam(value = "limit") Integer pageSize){
        PageInfo<TimeDimension> list= timeDimensionService.selectByDimensionList(timeDimension,pageIndex,pageSize);
        ReturnResult<TimeDimension> result = new ReturnResult<TimeDimension>(0,"成功",list.getList(),list.getTotal());
        return result;
    }

    /**
     * 新增页
     * @return
     */
    @GetMapping("addlatpage")
    public String addLat(){
        return "/timedimension/addlat";
    }

    /**
     *  新增方法
     * @return
     */
    @ApiOperation(value = "新增时间维度",notes = "新增时间维度")
    @PostMapping("adddimension")
    public Object addTimeDimension(TimeDimension timeDimension){
        redisServer.set(RedisKeysUtils.LOGINNAME,"新增时间维度");
        TimeDimension dimension = timeDimensionService.selectByEntity(timeDimension);
        if(null != dimension){
            return "/timedimension/timelat";
        }
        timeDimension.setCreateTime(new Date());
        timeDimensionService.insertSelective(timeDimension);
        return "/timedimension/timelat";
    }

    /**
     * 时间维度编辑页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("editdimension")
    public String editTimeDimension(@RequestParam("id")Integer id,Model model){
        TimeDimension tds = timeDimensionService.selectByPrimaryKey(id);
        model.addAttribute("tds",tds);
        return "/timedimension/editlatepage";
    }

    /**
     * 时间维度编辑方法
     * @param timeDimension
     * @return
     */
    @ApiOperation(value = "编辑时间维度",notes = "编辑时间维度")
    @PostMapping("updatedimension")
    public Object updateTimeDimension(TimeDimension timeDimension){
        redisServer.set(RedisKeysUtils.LOGINNAME,"编辑时间维度");
        try {
            timeDimension.setUpdateTime(new Date());
            int count = timeDimensionService.updateByPrimaryKey(timeDimension,timeDimensionService.selectByPrimaryKey(timeDimension.getId()));
            //中间表名称跟着修改
            ExchangeDimensionMiddle edm = new ExchangeDimensionMiddle();
            edm.setDimensionName(timeDimension.getDimensionName());
            edm.setTimeDimensionId(timeDimension.getId());
            exchangeDimensionMiddleService.updateByName(edm);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "/timedimension/timelat";
    }
}
